#ifndef SENSOR_H
#define SENSOR_H
#include <string.h>
#include <omnetpp.h>
#include "ArpeesDef.h"
#include "MessageType_m.h"
#include <math.h>
#include "BaseStation.h"



class Sensor: public cSimpleModule {
public :
    virtual void generateMessage2BS();
    bool hasToBSGate;
    double energy,energyMax;
    double distance2Event;
    int status;
    int active;
    double distance2BS,p;
    int xpos, ypos;
    int numberAckMax;
    int numberSttInfoMax;
    int numberSttInfo;
    //dung cho lua chon CH tu cac candiate
    int messCountMax,messCountCurr;
    double tempValue;
    cGate *chInGate;
    cArray *clusterMember;
    //
    cGate *minAckGate;
    double minAckValue;
    cGate *minAckCmpGate;
    double minAckCmpValue;

    //control isCH
    bool controlCH;


    int numberClusterMember;
    bool isCH,isFindRN;
    //
    //join to cluster
    int joinCHCurr,joinCHMax;

    //thiet lap chon CH theo arpees
    int isInEvent;
    //DATA 2 CH

    int numberFrame;
    int numberDataInCH;
    int currNumberData2CH;

    //truyen ve BS
    int relayDataCount;

    int nodeInEvent,currRound;
    BaseStation *base;

    //
    bool isColorize;
    // mleach
    int relayNodeGateId;

    virtual void broadCastStatusInfo();
    virtual void initNodes();

    virtual void temp();
    virtual void broadCastMess(cMessage *msg,int code);

    virtual void handleClusterHeadAdv(cMessage *mess);
    virtual void handleClusterHeadJoin(cMessage *mess);
    virtual void handleTDMA(cMessage *mess);
    virtual void handleStatusInfo(cMessage *mess);
    virtual void handleData2CH_self(cMessage *mess);
    virtual void handleData2CH_notself(cMessage *mess);
    virtual void handleData2RN(cMessage *mess);
    virtual double distanceCal(double x1,double y1,double x2,double y2);
    virtual double distanceCal(Sensor *sensor);
    virtual void resetRound();
    virtual void generateEvent();
    virtual void hanndleMyGenEvent(cMessage *mess);
    virtual void myScheduleAt(simtime_t t,cMessage *msg);

protected:

    int id;
    int numberNeighbor,protocol;
    double k1,k2,k3,k4;
    int transmitRange,sensorRange;

    double pixel2meterRatio;



    cGate *toBSGate;
    //Xu li data to relay node

    Data2RNMessage *data2RNMessage;
    cGate *relayNodeGate;
    cModule *nodePtr[NUMBERNODES + 4];

    //use for test
   // cArray *gateOutArr;

    //use for elec relay node
    cArray *ackRNMessArray;
    int numberAck;
    //AckRN *minAckMess;
    //AckRN *minCompareAckMess;

    virtual void initialize();
    virtual void handleMessage(cMessage *msg);
    virtual void finish();

    virtual void broadcastRequestRNMess();
    virtual void sendAckRNMess(cGate *gate,cMessage* mess);
    virtual cGate* getOutGate(cGate* gateIn);
    virtual cGate* getInGate(Sensor* sensor);
    virtual void energyCalculate(int messKind,int transmitType,double distance);
    virtual double energyTool(int messKind,int transmitType,double distance);
    virtual void mySend(cMessage *mess, cGate *gate,bool isCalEnergy=true);


};
#endif

